package com.dmap.admin.service.backgroundv2.impl;

import com.dmap.admin.constants.AdminConstant;
import com.dmap.admin.controller.backgroundv2.param.DeviceTypeParam;
import com.dmap.admin.controller.backgroundv2.param.DeviceTypeSelectParam;
import com.dmap.admin.dao.DeviceTypeMapper;
import com.dmap.admin.po.DeviceType;
import com.dmap.admin.po.DeviceTypeExample;
import com.dmap.admin.service.backgroundv2.DeviceTypeService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * \* User: helei
 * \* Date: 25/05/18
 * \* Description:设备类型相关接口实现类
 * \
 */
@Service
public class DeviceTypeServiceImpl implements DeviceTypeService {

    @Resource
    private DeviceTypeMapper deviceTypeMapper;

    @Override
    public DeviceType insertDeviceType(DeviceTypeParam param) {
        DeviceType deviceType = new DeviceType();
        deviceType.setTypeNumber(param.getTypeNumber());
        deviceType.setTypeName(param.getTypeName());
        deviceType.setTypeImageUrl(param.getTypeImageUrl());
        deviceType.setStatus(AdminConstant.NORMAL_STATUS);
        deviceType.setNote(param.getNote());
        deviceType.setCreateTime(System.currentTimeMillis() / 1000);
        deviceType.setUpdateTime(deviceType.getCreateTime());
        int success = deviceTypeMapper.insertSelective(deviceType);
        return (success == 1) ? deviceType : null;
    }

    @Override
    public boolean updateDeviceType(DeviceTypeParam param) {
        DeviceType deviceType = new DeviceType();
        deviceType.setId(Long.valueOf(param.getId()));
        deviceType.setTypeNumber(param.getTypeNumber());
        deviceType.setTypeName(param.getTypeName());
        deviceType.setTypeImageUrl(param.getTypeImageUrl());
        deviceType.setNote(param.getNote());
        deviceType.setUpdateTime(System.currentTimeMillis() / 1000);
        return deviceTypeMapper.updateByPrimaryKeySelective(deviceType) == 1;
    }

    @Override
    public boolean validateNumberOrNameIsExist(String typeNumber, String typeName) {
        boolean returnValue = true;
        DeviceTypeExample example = new DeviceTypeExample();
        //匹配 类型编号 进行查询
        DeviceTypeExample.Criteria criteria1 = example.createCriteria();
        //匹配 类型名称 进行查询
        DeviceTypeExample.Criteria criteria2 = example.createCriteria();
        if (StringUtils.isNotBlank(typeNumber)) {
            criteria1.andTypeNumberEqualTo(typeNumber);
        }
        if (StringUtils.isNotBlank(typeName)) {
            criteria2.andTypeNameEqualTo(typeName);
        }
        example.or(criteria2);
        List<DeviceType> list = deviceTypeMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            returnValue = false;
        }
        return returnValue;
    }

    @Override
    public DeviceType selectById(Long id) {
        return deviceTypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<DeviceType> selectByDeviceTypeSelectParam(DeviceTypeSelectParam param) {
        DeviceTypeExample example = this.getDeviceTypeSelectParamExample(param.getSearchKeyword());
        //根据前端所传参数不同生成对应的orderBy子句
        String orderByClause = AdminConstant.getOrderByClauseMap().getOrDefault(param.getSortField(), AdminConstant.UPDATE_TIME)
                + AdminConstant.getOrderByClauseSequenceMap().getOrDefault(param.getOrder(), AdminConstant.DESC);
        example.setOrderByClause(orderByClause);
        example.setLimitStart((param.getPageIndex() - 1) * param.getPageSize());
        example.setLimitEnd(param.getPageSize());
        return deviceTypeMapper.selectByExample(example);
    }

    @Override
    public int countByDeviceTypeSelectParam(DeviceTypeSelectParam param) {
        DeviceTypeExample example = this.getDeviceTypeSelectParamExample(param.getSearchKeyword());
        return deviceTypeMapper.countByExample(example);
    }

    private DeviceTypeExample getDeviceTypeSelectParamExample(String searchKeyword) {
        DeviceTypeExample example = new DeviceTypeExample();
        //根据 类型编号 模糊搜索
        DeviceTypeExample.Criteria criteria1 = example.createCriteria();
        //根据 类型名称 模糊搜索
        DeviceTypeExample.Criteria criteria2 = example.createCriteria();
        if (StringUtils.isNotBlank(searchKeyword)) {
            criteria1.andTypeNumberLike("%" + searchKeyword + "%");
            criteria2.andTypeNameLike("%" + searchKeyword + "%");
        }
        example.or(criteria2);
        return example;
    }

}